DEW.AddUser = Backbone.View.extend({

    template : Handlebars.templates['add-user'],

    events: {
        'click .enviar-add-user' : 'addUser'
    },

    render: function(){
        this.$el.html(this.template({}));
    },

    addUser: function(event) {
        var me = this;
        event.preventDefault();

        var name = this.$el.find("#inputName").val();
        var email = this.$el.find("#inputEmail").val();
        var password = this.$el.find("#inputPassword").val();

        if (name !== "" && email !== "" && password !== "") {
            me.showErrorMessage(""); //Limpiar mensaje the error
            $.post(DEW.UrlManager.buildURL("addUser",{
                    keycode: DEW.keycode
                }),
            {
                password: password,
                name: name,
                email: email
            },function(json) {
                if (json.success) {
                    document.location = DEW.appURL + "/home?keycode=" + DEW.keycode;
                } else {
                    me.showErrorMessage(json.message);
                }
            }).fail(function() {
                me.showErrorMessage("Lo sentimos, hubo un error al enviar tu información. Vuelve a intentarlo.");
            });
        } else {
            me.showErrorMessage("Tiene que ingresar información");
        }
    },

    showErrorMessage: function(message) {
        this.$el.find(".message-error").html(message);
        this.$el.find(".message-error").show();
    }

});